package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BaseException;
import com.sky.exception.DataExistsException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.sky.constant.StatusConstant.DISABLE;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {
    private final CategoryMapper categoryMapper;
    private final SetmealMapper setmealMapper;
    private final DishMapper dishMapper;

    /**
     * 新增分类
     *
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        //通过分类名称查询分类，如果存在就抛出异常
        long count = categoryMapper.getCountByName(categoryDTO.getName());
        if (count != 0) {
            throw new DataExistsException("菜品已存在");
        }
        //将categoryDTO转换为Category对象
        Category category = BeanUtil.copyProperties(categoryDTO, Category.class);
        //属性赋值
        category.setStatus(DISABLE);
        //保存到数据库
        categoryMapper.save(category);
    }

    /**
     * 菜品分页查询
     *
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult categoryPage(CategoryPageQueryDTO pageQueryDTO) {
        //设置分页默认显示
        if (pageQueryDTO.getPage() == null || pageQueryDTO.getPageSize() == null) {
            pageQueryDTO.setPage(1);
            pageQueryDTO.setPageSize(10);
        }
        //设置分页参数
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        //查询数据
        Page<Category> list = categoryMapper.getCategoryList(pageQueryDTO);
        //返回分页结果
        return new PageResult(list.getTotal(), list.getResult());
    }

    /**
     * 删除分类
     */
    @Override
    public void deleteById(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        long count = dishMapper.getCountCategoryId(id);
        if (count != 0) {
            throw new BaseException("分类已关联菜品，不能删除");
        }
        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        long setmealMapperCountCategoryId = setmealMapper.getCountCategoryId(id);
        if (setmealMapperCountCategoryId != 0) {
            throw new BaseException("分类已关联套餐，不能删除");
        }
        //删除分类
        categoryMapper.deleteById(id);

    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        //对象拷贝
        Category category = BeanUtil.copyProperties(categoryDTO, Category.class);
        //设置属性
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        //判断套餐名称是否重复
        Category categoryById = categoryMapper.getCategoryById(category.getId());
        if (categoryById.getName().equals(category.getName())) {
            //更新分类
            categoryMapper.update(category);
        } else {
            //查询分类名称是否重复
            long count = categoryMapper.getCountByName(category.getName());
            if (count != 0) {
                throw new DataExistsException("分类名称已存在");
            }
            //更新分类
            categoryMapper.update(category);
        }

    }

    /**
     * 启用或停用分类
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        categoryMapper.startOrStop(status, id);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }
}
